/**********************************************************************/
/*
   Author: Karan Makkar
   Created: Nov 2023
   Description: Inputs into the Lottery Weights
   Output: TableA1
*/
/**********************************************************************/

/*----------------------------------------------------*/
* Section: Setup
/*----------------------------------------------------*/

* include filepaths 
  if "$master_run" !="1" include "./Do/SET_FILEPATHS.do"

  clear all
  set more off
  set matsize 11000

* Log
  cap log close
  global prefix: display %tdCYND td(`c(current_date)')
  log using "$KP_logs/${prefix}_lottery_inputs_reg.txt", text replace

/*----------------------------------------------------*/
* Section: Clean Data
/*----------------------------------------------------*/

u "$KP_deid_admin/Clean/pmo_b1-22_clean_long_deid.dta", clear

gen educ_senhigh = yrs_school >= 12 & !mi(yrs_school) 

* Age, test score dummies
gen age_above25 = age >= 25 if !mi(age)
gen below25 = age < 25 // dummy for age < 25
gen score_60_80 = inrange(final_test_score, 61, 80) if !mi(final_test_score)
gen score_80 = final_test_score > 80 if !mi(final_test_score)

gen aaa_covid = inlist(1, aaa4, aaa7, aaa9, aaa25, aaa26, aaa27, aaa36, aaa37, aaa38) // covid control
gen aaa_unemp = inlist(1, aaa1, aaa20, aaa31) // is unemployed

// change covid control for batches 2 and 3
replace aaa_covid = inlist(1, aaa4, aaa7, aaa9) | (aaa1 & below25) if inlist(batch, 2, 3)

gen lost = win_in_batch == 0
bys anon_id4 (batch): gen entered_prev_nd_lost = sum(lost)-1 
bys anon_id4 (batch): replace entered_prev_nd_lost = entered_prev_nd_lost+1 if lost == 0
sum entered_prev_nd_lost

* Drop non-randomized batches
drop if inlist(batch, 1, 15)

/*----------------------------------------------------*/
* Section: Make Table
/*----------------------------------------------------*/
label var aaa_unemp "Unemployed"
label var aaa_covid "Covid Impact Employment/Hrs Worked/Business"
label var age_above25 "Age $\geq$ 25"
label var educ_senhigh "Years of education $\geq$ 12"
label var score_60_80 "Test Score 60-80"
label var score_80 "Test Score $>$ 80"
label var entered_prev_nd_lost "Num Times Lost Lottery"

eststo t2: reghdfe bobot aaa_unemp aaa_covid age_above25 educ_senhigh score_60_80 score_80 entered_prev_nd_lost, vce(cluster anon_id4) absorb(batch anon_prov_id)

esttab t2 using $KP_output/tables/descriptive/tex/TableA1.tex, ///
b(%9.3f) se(%9.3f) ///
stats(N, label("Observations") fmt(%10.0fc)) ///
drop(_cons) ///
collabels(none) ///
label ///
star(* 0.10 ** 0.05 *** 0.01) /// 
mtitle("Lottery Weights") ///
prehead(`"\hline \hline"') ///
prefoot(`"\\ "') ///
postfoot(`"\hline"') ///
nonum ///
replace
